using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class JoystickUI : MonoBehaviour
{
    public RectTransform background; // 摇杆背景
    public RectTransform m_handle;     // 摇杆手柄
    public Vector2 inputDirection;   // 输入方向
    public float InputPercent;
    
    public Train train;  // 引用Train组件
    public TouchMethod TouchMethod;

    private Vector2 input;
    private Vector2 centerPosition;

    private bool IsDown;

    void Start()
    {
        Debug.Log("触摸组件开始");
        centerPosition = m_handle.anchoredPosition;
        background.gameObject.SetActive(false);

        TouchMethod.PointerDownEvent += OnPointerDownEvent;
        TouchMethod.PointerUpEvent += OnPointerUpEvent;
        TouchMethod.PointerDragEvent += OnPointerDragEvent;
    }

    private void OnPointerDownEvent(PointerEventData pointerEventData)
    {
        IsDown = true;
        background.gameObject.SetActive(true);
        background.anchoredPosition = Vector2.zero;
        Vector2 mousePosition = pointerEventData.position;    
        Vector2 localMousePosition;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(background,mousePosition,null,out localMousePosition);
        background.anchoredPosition = localMousePosition;
    }

    private void OnPointerUpEvent(PointerEventData pointerEventData)
    {
        IsDown = false;
        background.gameObject.SetActive(false);
        background.anchoredPosition = new Vector2(0,-450);
        input = Vector2.zero;
        inputDirection = Vector2.zero;
        background.anchoredPosition = Vector2.zero;
        m_handle.anchoredPosition = Vector2.zero;
        
        // 摇杆释放时不需要发送方向
        if (train != null)
        {
            train.SetDirection(Vector2.zero);
        }
    }

    private void OnPointerDragEvent(PointerEventData pointerEventData)
    {
        Vector2 DragPosition = pointerEventData.position;
        Vector2 LocalDragPosition;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(background,DragPosition,null,out LocalDragPosition);
        
        float radius = background.rect.width / 2;
        input = DragPosition - (Vector2)background.position;
        Vector2 inputNormalized = new Vector2(input.x, input.y);
        float distance = inputNormalized.magnitude;
        
        if (distance > radius)
        {
            distance = radius;
            inputNormalized = inputNormalized.normalized * radius;
        }

        InputPercent = distance / radius;
        InputPercent = Mathf.Max(0.6f, InputPercent);
        m_handle.anchoredPosition = inputNormalized;
        
        // 计算方向并发送给火车
        if (distance > radius * 0.1f) // 添加死区
        {
            inputDirection = (m_handle.position - background.position).normalized;
            if(train != null)
            {
                train.SetDirection(inputDirection);
            }
        }
        else
        {
            inputDirection = Vector2.zero;
            if(train != null)
            {
                train.SetDirection(Vector2.zero);
            }
        }
    }

    private float DownTime = 0;
}

